/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.equipment.entity.EleApplication;
import com.thinkgem.jeesite.modules.equipment.service.EleApplicationService;

/**
 * 应用管理Controller
 * @author ZC
 * @version 2017-05-10
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleApplication")
public class EleApplicationController extends BaseController {

	@Autowired
	private EleApplicationService eleApplicationService;
	
	@ModelAttribute
	public EleApplication get(@RequestParam(required=false) String id) {
		EleApplication entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleApplicationService.get(id);
		}
		if (entity == null){
			entity = new EleApplication();
		}
		return entity;
	}
	
	@RequiresPermissions("equipment:eleApplication:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleApplication eleApplication, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleApplication> page = eleApplicationService.findPage(new Page<EleApplication>(request, response), eleApplication); 
		model.addAttribute("page", page);
		return "modules/equipment/eleApplicationList";
	}

	@RequiresPermissions("equipment:eleApplication:view")
	@RequestMapping(value = "form")
	public String form(EleApplication eleApplication, Model model) {
		model.addAttribute("eleApplication", eleApplication);
		return "modules/equipment/eleApplicationForm";
	}

	@RequiresPermissions("equipment:eleApplication:edit")
	@RequestMapping(value = "save")
	public String save(EleApplication eleApplication, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleApplication)){
			return form(eleApplication, model);
		}
		eleApplicationService.save(eleApplication);
		addMessage(redirectAttributes, "保存应用成功");
		return "redirect:"+Global.getAdminPath()+"/equipment/eleApplication/?repage";
	}
	
	@RequiresPermissions("equipment:eleApplication:edit")
	@RequestMapping(value = "delete")
	public String delete(EleApplication eleApplication, RedirectAttributes redirectAttributes) {
		eleApplicationService.delete(eleApplication);
		addMessage(redirectAttributes, "删除应用成功");
		return "redirect:"+Global.getAdminPath()+"/equipment/eleApplication/?repage";
	}

}